/**
 * Created on 2007-05-17 18:30
 */
package game.frames;

import internet.InternetReader;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import game.score.ScoreManager;

/**
 * @author David Wallin
 */
public class NameEntryFrame extends BasicFrame
{
	/**
	 * Serializable interface forces this to be declared
	 */
	private static final long		serialVersionUID = -6346359295823503682L;
	private JLabel					m_Header;
	private JLabel					m_Score;
	private JLabel					m_Name;
	private JTextField				m_NameEntry;
	private JButton					m_Submit;
	
	/**
	 * 
	 */
	public NameEntryFrame()
	{
		super(new GridBagLayout());
	}
	
	/* (non-Javadoc)
	 * @see frames.BasicFrame#initializeComponent()
	 */
	@Override
	protected void initializeComponent()
	{
		this.setBackground(Color.black);
		GridBagLayout gridBag = (GridBagLayout)this.getLayout();
		
		GridBagConstraints headerConstraints = new GridBagConstraints();
		headerConstraints.insets = new Insets(10, 0, 0, 0);
		headerConstraints.gridx = 1;
		headerConstraints.gridy = 0;
		headerConstraints.gridwidth = 1;
		headerConstraints.weightx = 0.5;
		
		GridBagConstraints scoreConstraints = new GridBagConstraints();
		scoreConstraints.gridx = 0;
		scoreConstraints.gridy = 1;
		scoreConstraints.gridwidth = 3;
		
		GridBagConstraints nameConstraints = new GridBagConstraints();
		nameConstraints.gridx = 0;
		nameConstraints.gridy = 2;
		nameConstraints.gridwidth = 3;
		
		GridBagConstraints nameEntryConstraints = new GridBagConstraints();
		nameEntryConstraints.gridx = 0;
		nameEntryConstraints.gridy = 3;
		nameEntryConstraints.gridwidth = 3;
		
		GridBagConstraints submitConstraints = new GridBagConstraints();
		submitConstraints.gridx = 1;
		submitConstraints.gridwidth = 1;
		submitConstraints.weightx = 0.5;
		submitConstraints.weighty = 1.5;
		submitConstraints.anchor = GridBagConstraints.SOUTH;
		
		m_Header = new JLabel("Game Over");
		m_Score = new JLabel("You got " + ScoreManager.INSTANCE.getRealScore() + " points, and reached level " + ScoreManager.INSTANCE.getLevel() + ".");
		m_Name = new JLabel("Please enter your name for the record:");
		m_NameEntry = new JTextField();
		m_Submit = new JButton("Submit");
		
		Font f = new Font("Times", Font.BOLD, 14);
		m_Header.setFont(f);
		m_Header.setHorizontalAlignment(JLabel.CENTER);
		m_Header.setVerticalAlignment(JLabel.CENTER);
		m_Header.setBackground(Color.black);
		m_Header.setForeground(Color.gray);
		
		f = new Font("Times", Font.PLAIN, 12);
		m_Score.setFont(f);
		m_Score.setHorizontalAlignment(JLabel.CENTER);
		m_Score.setVerticalAlignment(JLabel.CENTER);
		m_Score.setBackground(Color.black);
		m_Score.setForeground(Color.gray);
		
		m_Name.setFont(f);
		m_Name.setHorizontalAlignment(JLabel.CENTER);
		m_Name.setVerticalAlignment(JLabel.CENTER);
		m_Name.setBackground(Color.black);
		m_Name.setForeground(Color.gray);
		
		m_NameEntry.setColumns(20);
		m_NameEntry.addActionListener(this);
		m_Submit.addActionListener(this);
		
		this.addComponent(m_Header, gridBag, headerConstraints);
		this.addComponent(m_Score, gridBag, scoreConstraints);
		this.addComponent(m_Name, gridBag, nameConstraints);
		this.addComponent(m_NameEntry, gridBag, nameEntryConstraints);
		this.addComponent(m_Submit, gridBag, submitConstraints);
		
		this.doLayout();
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		Object source = event.getSource();
		
		if((source == m_Submit) || (source == m_NameEntry))
		{
			this.addHighscore();
			m_NameEntry.setText("");
			this.updateFrameSwitchListeners(Frames.HIGHSCORES);
		}
	}
	
	public void setScoreAndLevel()
	{
		m_Score.setText("You got " + ScoreManager.INSTANCE.getRealScore() + " points, and reached level " + ScoreManager.INSTANCE.getLevel() + ".");
	}
	
	private void addHighscore()
	{
		int					score = ScoreManager.INSTANCE.getRealScore();
		int					level = ScoreManager.INSTANCE.getLevel();
		String				name = m_NameEntry.getText();
		Date				time = new Date();
		String				encoding = "utf8";
		String				result = "";
		
		m_InputManager.setHighscoreAdded(false);
		
		try
		{
			String urlAddress = InternetReader.getHighscoreSubmitLocation();
			urlAddress += ("&name=" + URLEncoder.encode(name, encoding));
			urlAddress += ("&score=" + URLEncoder.encode(String.valueOf(score), encoding));
			urlAddress += ("&level=" + URLEncoder.encode(String.valueOf(level), encoding));
			urlAddress += ("&date=" + URLEncoder.encode(this.parseDate(time), encoding));
			urlAddress += ("&time=" + URLEncoder.encode(this.parseTime(time), encoding));
			urlAddress += ("&difficulty=" + URLEncoder.encode(m_Settings.getDifficulty().toString(), encoding));
			
			InternetReader internetReader = new InternetReader(urlAddress);
			internetReader.loadDocument();
			result = internetReader.readLine();
			
			if(result.equals("OK"))
			{
				m_InputManager.setHighscoreAdded(true);
			}
		}
		catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		catch(IllegalAccessException e)
		{
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private String parseDate(Date date)
	{
		Timestamp t = new Timestamp(date.getTime());
		String s = t.toString();
		
		return s.substring(2, 10);
	}
	
	private String parseTime(Date date)
	{
		Timestamp t = new Timestamp(date.getTime());
		String s = t.toString();
		
		return s.substring(11, 19);
	}
}
